package ui.components.renderlist
{
	import events.ScrollEvent;
	
	import flash.display.Sprite;
	
	import utils.IClear;
	import utils.PanelUtil;
	import utils.ScrollPanel;
	import utils.group.SelectGroup;
	
    public class RenderList extends Sprite implements IClear
    {
        private var _listHeight:uint;
        private var _rendererSprite:Sprite;
        private var _bgSprite:Sprite;
        private var _listWidth:uint;
        private var _itemNumHorizon:int = 0;
        private var _dataProvider:Array;
        private var _itemNumVerticle:int = 0;
        private var _rendererList:Array;
        private var _selectGroup:SelectGroup;
        private var _scrollPanel:ScrollPanel;
        
        public function RenderList(param1:uint = 0, param2:uint = 0, param3:Array = null)
        {
            _listWidth = param1;
            _listHeight = param2;
            _dataProvider = param3;
            init();
        }

        public function resetSelection() : void
        {
            if (currentRenderer != null)
            {
                currentRenderer.selected = false;
            }
        }

        private function onScroll(event:ScrollEvent):void
        {
            
        }

        public function getRendererAt(param1:int) : AbstractRenderer
        {
            return _rendererList[param1];
        }

        private function drawLayout() : void
        {
            var _loc_3:int = 0;
            if (_rendererList == null || _rendererList.length <= 0)
            {
                return;
            }
            var _loc_1:* = _rendererList[0] as AbstractRenderer;
            if (_itemNumHorizon > 0)
            {
                _listWidth = _itemNumHorizon * _loc_1.width + 22;
            }
            if (_itemNumVerticle > 0)
            {
                _listHeight = _itemNumVerticle * (_loc_1.height + 2) - 2;
            }
            _scrollPanel.width = _listWidth;
            _scrollPanel.height = _listHeight;
            var _loc_2:int = 0;
            while (_loc_2 < _rendererList.length)
            {
                
                _loc_1 = _rendererList[_loc_2] as AbstractRenderer;
                _loc_3 = _listWidth / _loc_1.width;
                if (_loc_3 < 1)
                {
                    _loc_3 = 1;
                }
                _loc_1.x = _loc_2 % _loc_3 * (_loc_1.width + 2);
                _loc_1.y = Math.floor(_loc_2 / _loc_3) * (_loc_1.height + 2);
                _loc_2++;
            }
        }

        public function get currentRenderers() : Array
        {
            return _selectGroup.currentItems;
        }

        public function get verticalLineScrollSize() : Number
        {
            return _scrollPanel.verticalLineScrollSize;
        }

        private function createScrollPanel() : void
        {
            _scrollPanel = new ScrollPanel();
            _scrollPanel.width = _listWidth;
            _scrollPanel.height = _listHeight;
            _scrollPanel.source = _rendererSprite;
            _scrollPanel.addEventListener(ScrollEvent.SCROLL, onScroll);
            PanelUtil.drawBackground(_scrollPanel, 0, 0);
            addChild(_scrollPanel);
        }

        public function get currentRenderer():AbstractRenderer
        {
            var _loc_1:* = _selectGroup.currentItem as AbstractRenderer;
            if (_loc_1 == null || !_loc_1.value.selectable)
            {
                return null;
            }
            return _loc_1;
        }

        public function set verticalLineScrollSize(param1:Number) : void
        {
            _scrollPanel.verticalLineScrollSize = param1;
            return;
        }

        public function get itemNumVerticle() : int
        {
            return _itemNumVerticle;
        }

        private function setSelectGroup() : void
        {
            var _loc_1:* = _dataProvider[0] as AbstractRendererVo;
            _selectGroup.currentType = _loc_1 == null ? (SelectGroup.SINGLE_SELECT) : (_loc_1.selectType);
        }

        private function updateView() : void
        {
            _scrollPanel.verticalScrollPosition = 0;
            setSelectGroup();
            removeAllItem();
            createRenderer();
        }

        private function createRenderer() : void
        {
            var _loc_1:uint = 0;
            var _loc_2:uint = 0;
            var _loc_3:AbstractRendererVo = null;
            var _loc_4:AbstractRenderer = null;
            var _loc_5:int = 0;
            if (_dataProvider != null)
            {
                _selectGroup.clear();
                _loc_1 = _dataProvider.length;
                _rendererList.splice(0, _rendererList.length);
                _loc_2 = 0;
                while (_loc_2 < _loc_1)
                {
                    
                    _loc_3 = _dataProvider[_loc_2] as AbstractRendererVo;
                    _loc_4 = RendererPool.borrowRenderer(_loc_3.rendererClass);
                    _loc_4.update(_loc_3);
                    _loc_5 = _listWidth / _loc_4.width;
                    if (_loc_5 < 1)
                    {
                        _loc_5 = 1;
                    }
                    _loc_4.x = _loc_2 % _loc_5 * (_loc_4.width + 2);
                    _loc_4.y = Math.floor(_loc_2 / _loc_5) * (_loc_4.height + 2);
                    _loc_4.addEventListener(RendererEvent.RENDERER_CLICK, rendererHandler);
                    _loc_4.addEventListener(RendererEvent.RENDERER_OUT, rendererHandler);
                    _loc_4.addEventListener(RendererEvent.RENDERER_OVER, rendererHandler);
                    if (_loc_3.selectable)
                    {
                        _selectGroup.addItem(_loc_4);
                    }
                    _rendererSprite.addChild(_loc_4);
                    _rendererList.push(_loc_4);
                    _loc_2 = _loc_2 + 1;
                }
            }
            if (_rendererList.length > 0)
            {
                _scrollPanel.verticalLineScrollSize = _rendererList[0].height + 2;
                drawLayout();
            }
            _scrollPanel.update();
        }

        public function get itemNumHorizon() : int
        {
            return _itemNumHorizon;
        }

        private function rendererHandler(event:RendererEvent) : void
        {
            var _loc_2:AbstractRendererVo = null;
            switch(event.type)
            {
                case RendererEvent.RENDERER_CLICK:
                {
                    _loc_2 = event.data as RendererEventVo;
                    dispatchEvent(new RenderListEvent(_loc_2));
                    break;
                }
                case RendererEvent.RENDERER_OUT:
                {
                    break;
                }
                case RendererEvent.RENDERER_OVER:
                {
                    break;
                }
                default:
                {
                    break;
                    break;
                }
            }
        }

        public function setDataProvider(param1:Array) : void
        {
            _dataProvider = param1;
            updateView();
        }
        
        public function setListSize(width:uint, height:uint):void {
        	_listWidth = width;
        	_listHeight = height;
        }

        public function set itemNumVerticle(param1:int) : void
        {
            _itemNumVerticle = param1;
            drawLayout();
        }

        public function getDataProvider() : Array
        {
            return _dataProvider;
        }

        public function set itemNumHorizon(param1:int) : void
        {
            _itemNumHorizon = param1;
            drawLayout();
        }

        public function removeAllItem() : void
        {
            var _loc_1:AbstractRenderer = null;
            _scrollPanel.verticalScrollPosition = 0;
            while (_rendererSprite.numChildren > 0)
            {
                
                _loc_1 = _rendererSprite.getChildAt(0) as AbstractRenderer;
                _rendererSprite.removeChild(_loc_1);
                RendererPool.returnRenderer(_loc_1);
            }
            _scrollPanel.update();
        }

        public function get currentValue() : AbstractRendererVo
        {
            var _loc_1:* = currentRenderer;
            return _loc_1 == null ? (null) : (_loc_1.value);
        }

        public function get currentValues() : Array
        {
            var _loc_3:AbstractRenderer = null;
            var _loc_1:Array = [];
            var _loc_2:* = currentRenderers;
            for each (_loc_3 in _loc_2)
            {
                
                _loc_1.push(_loc_3.value);
            }
            return _loc_1;
        }

        private function init() : void
        {
            _selectGroup = new SelectGroup();
            _rendererList = new Array();
            _rendererSprite = new Sprite();
            addChild(_rendererSprite);
            createScrollPanel();
            createRenderer();
        }
        
        public function clear():void {
        	removeAllItem();
        }
    }
}
